import 'package:flutter/material.dart';

import 'theme.dart';

class UiListCell extends StatelessWidget {
  final Widget? title;
  final Widget? leading;
  final double leadingSpace;
  final Widget? trailing;
  final Function()? onTap;
  final Function()? onDoubleTap;
  final Function()? onLongPress;
  final Color hoverColor;
  final Color? backgroundColor;
  final double opacity;
  final TextStyle? style;
  final bool showDivider;
  final EdgeInsetsGeometry margin;
  final EdgeInsetsGeometry padding;
  final bool showArrow;
  final Color? dividerColor;
  final double? dividerIndent;
  final double? dividerThickness;
  final String? to;
  final double minHeight;
  final bool selected;
  final Function(dynamic)? toResult;
  final BorderRadius? borderRadius;
  const UiListCell({
    super.key,
    this.title,
    this.leading,
    this.leadingSpace = 0,
    this.trailing,
    this.onTap,
    this.onDoubleTap,
    this.onLongPress,
    this.backgroundColor,
    this.hoverColor = Colors.grey,
    this.opacity = 0,
    this.style,
    this.to,
    this.minHeight = 50,
    this.showDivider = false,
    this.dividerIndent,
    this.dividerColor,
    this.dividerThickness,
    this.margin = EdgeInsets.zero,
    this.padding = const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
    this.showArrow = false,
    this.selected = false,
    this.toResult,
    this.borderRadius,
  });

  @override
  Widget build(BuildContext context) {
    double transparent = opacity;
    return StatefulBuilder(builder: ((context, setState) {
      return GestureDetector(
        onTapDown: (details) {
          if (onTap != null || to != null) {
            setState(
              () {
                transparent = 0.2;
              },
            );
          }
        },
        onTapUp: (details) {
          if (onTap != null || to != null) {
            setState(
              () {
                transparent = 0;
              },
            );
          }
        },
        onTapCancel: () {
          if (onTap != null || to != null) {
            setState(
              () {
                transparent = 0;
              },
            );
          }
        },
        onTap: () async {
          onTap?.call();
        },
        onDoubleTap: onDoubleTap,
        onLongPress: onLongPress,
        child: Container(
          color: backgroundColor ?? UiTheme(context).listCellBackgroundColor,
          child: Column(
            children: [
              Container(
                padding: padding,
                constraints: BoxConstraints(
                  minHeight: minHeight,
                ),
                decoration: BoxDecoration(
                  color: hoverColor.withOpacity(selected ? 0.2 : transparent),
                  borderRadius: borderRadius,
                ),
                child: Padding(
                  padding: margin,
                  child: DefaultTextStyle(
                    style: TextStyle(
                      fontSize: 16,
                      color: UiTheme.of(context).listCellForegroundColor,
                      height: 1.2,
                      fontFamily: UiTheme.fontFamily,
                      fontWeight: FontWeight.w300,
                    ),
                    child: Row(
                      children: [
                        leading ?? Container(),
                        if (leadingSpace > 0) SizedBox(width: leadingSpace),
                        Expanded(child: title ?? Container()),
                        trailing ?? Container(),
                        if (showArrow)
                          Padding(
                            padding: const EdgeInsets.only(left: 5.0),
                            child: Icon(
                              Icons.arrow_forward_ios,
                              size: 15,
                              color: UiTheme(context).listCellArrowColor,
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
              ),
              if (showDivider)
                Padding(
                  padding: margin,
                  child: Divider(
                    height: 0,
                    thickness: dividerThickness,
                    color: dividerColor,
                    indent: dividerIndent,
                  ),
                ),
            ],
          ),
        ),
      );
    }));
  }
}
